package org.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.example.springboot.annotations.MappingCombine;
import org.example.springboot.entities.Users;
import org.example.springboot.request.UserLoginRequest;
import org.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

@MappingCombine("/user")
public class UserController extends BaseController<UserService, Users> {
  @Value("${user.user-session}")
  public String USER_SESSION;

  @PostMapping("/login")
  public boolean login(@RequestBody UserLoginRequest user, HttpSession session) {
    if (baseService.doLogin(user)) {
      session.setAttribute(USER_SESSION, user);
      return true;
    }
    return false;
  }

  @PostMapping("/userInfo")
  public Users userInfo(String username) {
    return baseService.getOne(
        new LambdaQueryWrapper<Users>().
            eq(Users::getUsername, username)
    );
  }

  @PutMapping("/logout")
  public void logout(HttpSession session) {
    session.removeAttribute(USER_SESSION);
  }
}
